package com.sweetcircle.controller;


import com.sweetcircle.annotation.CheckObject;
import com.sweetcircle.annotation.GlobalInterceptor;
import com.sweetcircle.domain.dto.MessageSendDto;
import com.sweetcircle.domain.dto.TokenUserInfoDto;
import com.sweetcircle.domain.enums.MessageTypeEnum;
import com.sweetcircle.domain.po.ChatMessage;
import com.sweetcircle.domain.vo.ResponseVO;
import com.sweetcircle.exception.BusinessException;
import com.sweetcircle.service.ChatMessageService;
import com.sweetcircle.service.ChatSessionService;
import com.sweetcircle.utils.AnanUtils;
import com.sweetcircle.utils.RedisComponet;
import jakarta.annotation.Generated;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Max;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/chat")
public class ChatController extends ABaseController {

    private static final Logger logger = LoggerFactory.getLogger(ChatController.class);

    @Resource
    private ChatMessageService chatMessageService;

    @Resource
    private ChatSessionService chatSessionService;

    @Resource
    RedisComponet redisComponet;

    @RequestMapping("/sendMessage")
    @GlobalInterceptor(checkParams = true)
    public ResponseVO sendMessage(
            @CheckObject(required = true, checkProperties = {"messageContent", "messageType", "contactId","messageCode"})
            @RequestBody ChatMessage chatMessage,HttpServletRequest request) {


        TokenUserInfoDto tokenUserInfoDto = redisComponet.getTokenUserInfoDto(AnanUtils.getAuthorization(request));

        // MessageTypeEnum.FORCE_OFF_LINE.getType().equals(chatMessage.getMessageType()  判断 消息类型是否为强制下线
        // 如果不是异地登录 强制下线 那么就校验 是否登录或者是登录过期
        if (tokenUserInfoDto == null && !MessageTypeEnum.FORCE_OFF_LINE.getType().equals(chatMessage.getMessageType())) {
            throw new BusinessException(600, "用户未登录 或 登录超时 请登录！");
        }


        MessageSendDto messageSendDto = chatMessageService.saveMessage(chatMessage, tokenUserInfoDto);
        return successResponseVO(messageSendDto);
    }
}
